import numpy as np
import matplotlib.pyplot as plt

# Constants
pi = np.pi
k = 1.38e-23  # Boltzman's constant

# Parameters
pt = 1e+6  # peak power in Watts
freq = 5.6e+9  # radar operating frequency in Hz
g_db = 40.0  # antenna gain in dB
sigma = 0.1  # radar cross section in m squared
te = 300.0  # effective noise temperature in Kelvins
nf_db = 5.0  # noise figure in dB
loss_db = 6.0  # radar losses in dB
ranges = np.array([75e3, 100e3, 150e3])  # three range values in meters
snr_db = np.linspace(5, 20, 200)  # SNR values from 5 dB to 20 dB, 200 points
snr = 10**(0.1 * snr_db)  # convert snr into base 10
gain = 10**(0.1 * g_db)  # convert antenna gain into base 10
loss = 10**(0.1 * loss_db)  # convert losses into base 10
F = 10**(0.1 * nf_db)  # convert noise figure into base 10
lambda_ = 3e8 / freq  # compute wavelength

# Implement Eq.(1.57)
den = pt * gain * gain * sigma * lambda_**2
num1 = (4 * pi)**3 * k * te * F * loss * ranges[0]**4 * snr
num2 = (4 * pi)**3 * k * te * F * loss * ranges[1]**4 * snr
num3 = (4 * pi)**3 * k * te * F * loss * ranges[2]**4 * snr
tau1 = num1 / den
tau2 = num2 / den
tau3 = num3 / den

# Plot tau versus snr
plt.figure(1)
plt.semilogy(snr_db, 1e6 * tau1, 'k', snr_db, 1e6 * tau2, 'k-.', snr_db, 1e6 * tau3, 'k:')
plt.grid(True)
plt.legend(['R = 75 Km', 'R = 100 Km', 'R = 150 Km'])
plt.xlabel('Minimum required SNR - dB')
plt.ylabel(r'$\tau$ (pulsewidth) in $\mu sec$')
plt.show()
